import 'package:tile/tile.dart';
import 'package:architecture/os_size.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../base/base_chess_controller.dart';
import '../../base/theme_controller.dart';
import '../../util/level.dart';
import '../../widgets/drop_down_text.dart';
import '../../widgets/text_button.dart';
import 'ai_controller.dart';

class AIPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<AIController>(
        init: AIController(),
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(title: Text('AI相关')),
              body: Container(
                  width: windowWidth * 0.98,
                  margin: EdgeInsets.only(top: 15),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _textButton('导入棋谱', onPressed: controller.importSgf),
                          Container(
                              width: windowWidth * 0.35,
                              margin: EdgeInsets.only(top: 15),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  // Row(
                                  //   mainAxisAlignment:
                                  //       MainAxisAlignment.spaceBetween,
                                  //   children: [
                                  //     Text('棋谱信息'),
                                  //   ],
                                  // ),
                                  // SizedBox(
                                  //   height: 5,
                                  // ),
                                  // TextField(
                                  //   controller: controller.sgfEditingController,
                                  //   readOnly: true,
                                  //   decoration: InputDecoration(
                                  //     contentPadding: EdgeInsets.all(10),
                                  //     border: const OutlineInputBorder(),
                                  //   ),
                                  //   maxLines: 10,
                                  //   style: textStyle1,
                                  // ),
                                  Text(
                                    controller.result.value,
                                    style: TextStyle(color: Colors.red),
                                  ),
                                  Container(
                                      margin:
                                          EdgeInsets.symmetric(horizontal: 20),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text('自己'),
                                          Text(controller.aiName.value),
                                        ],
                                      )),
                                  _tileView(controller),
                                  SizedBox(height: 10),
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: [
                                      _button('悔棋', onPressed: () {
                                        controller.regret();
                                      }),
                                      _button('手顺', onPressed: () {
                                        controller.changeTileNum();
                                      }),
                                      _button('形势', onPressed: () {
                                        controller.situation();
                                      }),
                                      _button('数子', onPressed: () {
                                        controller.count();
                                      }),
                                      _button('推荐点', onPressed: () {
                                        controller.recommend();
                                      }),
                                      _button('变化图', onPressed: () {
                                        controller.hotPhoto();
                                      })
                                    ],
                                  ),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    children: [
                                      Text('跳转到第'),
                                      Container(
                                          height: 25,
                                          width: 40,
                                          margin: EdgeInsets.only(
                                              top: 15, left: 5, right: 5),
                                          child: TextField(
                                            controller:
                                                controller.numEditingController,
                                            cursorHeight: 12,
                                            decoration: InputDecoration(
                                              contentPadding: EdgeInsets.all(5),
                                              border:
                                                  const OutlineInputBorder(),
                                              // label: Text('请求和返回信息'),
                                            ),
                                            maxLines: 1,
                                            style: textStyle1,
                                          )),
                                      Text('步'),
                                      SizedBox(
                                        width: 10,
                                      ),
                                      _textButton('确定',
                                          height: 25,
                                          onPressed: controller.confirm),
                                    ],
                                  )
                                ],
                              ))
                        ],
                      ),
                      // SizedBox(
                      //   width: windowWidth * 0.1,
                      // ),
                      // Column(
                      //   mainAxisAlignment: MainAxisAlignment.center,
                      //   children: [
                      //     _textButton('推荐点', onPressed: () {
                      //       controller.hotPhoto();
                      //     }),
                      //     _textButton('变化图', onPressed: () {
                      //       controller.hotPhoto();
                      //     })
                      //   ],
                      // ),
                      SizedBox(
                        width: windowWidth * 0.05,
                      ),
                      Expanded(
                          child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          _aiType(controller),
                          Offstage(
                              offstage: controller.type.value != 1,
                              child: Row(
                                children: [
                                  Text(
                                    "选择等级:",
                                    style: TextStyle(color: Colors.black),
                                  ),
                                  SizedBox(
                                    width: 15,
                                  ),
                                  DropDownText(
                                      width: 150,
                                      height: 35,
                                      msg: levels
                                          .toList()[controller.level.value - 2],
                                      boardColor: Colors.black,
                                      list: levels.toList(),
                                      onPressed: (value) {
                                        controller.changeLevel(value);
                                      }),
                                ],
                              )),
                          Offstage(
                              offstage: controller.type.value != 4,
                              child: Row(
                                children: [
                                  Text(
                                    "选择种族:",
                                    style: TextStyle(color: Colors.black),
                                  ),
                                  SizedBox(
                                    width: 15,
                                  ),
                                  DropDownText(
                                      width: 150,
                                      height: 35,
                                      msg: controller.roleList.isEmpty
                                          ? ''
                                          : controller.roleList[
                                              controller.roleIndex.value],
                                      boardColor: Colors.black,
                                      list: controller.roleList,
                                      onPressed: (value) {
                                        controller.changeRole(value);
                                      }),
                                  SizedBox(
                                    width: 20,
                                  ),
                                  Offstage(
                                      offstage: controller.aiLevelList.isEmpty,
                                      child: Text(
                                        "选择等级:",
                                        style: TextStyle(color: Colors.black),
                                      )),
                                  SizedBox(
                                    width: 15,
                                  ),
                                  controller.aiLevelList.isEmpty
                                      ? Container()
                                      : DropDownText(
                                          width: 150,
                                          height: 35,
                                          msg: controller.aiLevelList[
                                              controller.aiLevelIndex.value],
                                          boardColor: Colors.black,
                                          list: controller.aiLevelList,
                                          onPressed: (value) {
                                            controller.changeAiLevel(value);
                                          }),
                                ],
                              )),
                          SizedBox(
                            height: 20,
                          ),
                          Text(
                            '请求和返回信息:',
                            style: TextStyle(color: Colors.black),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Expanded(
                              child: TextField(
                            controller: controller.resultEditingController,
                            readOnly: true,
                            decoration: InputDecoration(
                              contentPadding: EdgeInsets.symmetric(
                                  vertical: 20, horizontal: 10),
                              border: const OutlineInputBorder(),
                              // label: Text('请求和返回信息'),
                            ),
                            maxLines: 16,
                            style: textStyle1,
                          ))
                        ],
                      ))
                    ],
                  )));
        });
  }

  _tileView(AIController controller) {
    return Padding(
      padding: const EdgeInsets.only(left: 16.0, right: 0, top: 5, bottom: 6),
      child: TileView(
        controller.board,
        controller: controller.tileViewController,
        style: tileStyle,
      ),
    );
  }

  _aiType(AIController controller) {
    final radioWidth = windowWidth * 0.12;
    return Row(
      children: [
        Container(
            width: radioWidth,
            height: 50,
            child: RadioListTile<int>(
                title: const Text('星阵'),
                value: 1,
                groupValue: controller.type.value,
                onChanged: (int? newValue) {
                  controller.changeType(newValue!);
                })),
        Container(
            width: radioWidth,
            height: 50,
            child: RadioListTile<int>(
                title: const Text('leena'),
                value: 2,
                groupValue: controller.type.value,
                onChanged: (int? newValue) {
                  controller.changeType(newValue!);
                })),
        Container(
            width: radioWidth,
            height: 50,
            child: RadioListTile<int>(
                title: const Text('katago'),
                value: 3,
                groupValue: controller.type.value,
                onChanged: (int? newValue) {
                  controller.changeType(newValue!);
                })),
        Container(
            width: radioWidth,
            height: 50,
            child: RadioListTile<int>(
                title: const Text('十大种族'),
                value: 4,
                groupValue: controller.type.value,
                onChanged: (int? newValue) {
                  controller.changeType(newValue!);
                })),
      ],
    );
  }

  _textButton(String text, {double height = 35, Function? onPressed}) {
    return Container(
        margin: EdgeInsets.only(left: 15, right: 20),
        alignment: Alignment.centerLeft,
        child: MyTextButton(
            text: text,
            confirm: () {
              onPressed?.call();
            },
            height: height,
            backgroundColor: Colors.deepPurpleAccent,
            textStyle: TextStyle(color: Colors.white)));
  }

  _button(String text, {Function? onPressed}) {
    return Container(
        // margin: EdgeInsets.only(left: 10, right: 10),
        alignment: Alignment.centerLeft,
        child: MyTextButton(
            height: 25,
            text: text,
            confirm: () {
              onPressed?.call();
            },
            sideColor: Colors.deepPurpleAccent,
            backgroundColor: Colors.transparent,
            textStyle: TextStyle(color: Colors.deepPurpleAccent)));
  }

  _list(AIController controller) {
    final height = windowHeight * 0.05;
    return ListView.builder(
      itemCount: controller.roles.length,
      shrinkWrap: true,
      // padding: EdgeInsets.symmetric(horizontal: 10),
      itemBuilder: (context, index) {
        // bool isSelected = data.requireValue.selectedLevelPosition == index;
        final item = controller.roles[index];
        // Color color = isSelected
        //     ? const Color(0xFF2AC0AB)
        //     : (index % 2 == 0
        //         ? const Color(0xFFE1EFF0)
        //         : const Color(0xFFCCDBDC));
        return GestureDetector(
          onTap: () async {},
          child: Container(
            height: height * 1,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(height * 0.12),
              color: Colors.transparent,
            ),
            child: Text(
              item.name!,
              style: TextStyle(color: Colors.deepPurpleAccent),
            ),
          ),
        );
      },
    );
  }
}
